package Day2025_03_13;

public class LeetCode918 {
    public int maxSubarraySumCircular(int[] nums) {
        int n = nums.length;
        int sum = 0;
        for (int x : nums) sum += x;
        int[] f = new int[n];
        int[] g = new int[n];
        int[] h = new int[n];
        int fmax =nums[0], gmin = nums[0];
        f[0] = nums[0]; g[0] = nums[0];
        for (int i = 1; i < n; i++) {
            f[i] = Math.max(f[i - 1] + nums[i], nums[i]);
            fmax = Math.max(f[i], fmax);
            g[i] = Math.min(g[i - 1] + nums[i], nums[i]);
            gmin = Math.min(g[i], gmin);
        }
        return sum == gmin ? fmax : Math.max(fmax, sum - gmin);
    }
}
